package com.wfy.controller;

import com.wfy.pojo.Category;
import com.wfy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: CategoryController
 * @Package com.wfy.controller
 * @Author 王飞遥
 * @Date 2022/8/16 22:32
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryList(@RequestParam("pid") Long pid){
        if(pid==null || pid.longValue()<0){
            //如果不存在或者小于0
            //响应返回一个400
            return ResponseEntity.badRequest().build();
        }
        //查询数据库,获取数据
        List<Category> categoryList = categoryService.queryCategoryList(pid);
        if(CollectionUtils.isEmpty(categoryList)){
            //输出404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categoryList);
    }
    @GetMapping("/names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){
        List<String> strings = categoryService.queryNamesByIds(ids);
        if(CollectionUtils.isEmpty(strings)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(strings);
    }
    }
